package com.easyUploader.util;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author 大忽悠
 * @create 2022/11/2 10:10
 */
public class ContentNegotiationUtil {
    private static final Map<Set<String>, String> contentTypeMaps = new HashMap<>();

    static {
        contentTypeMaps.put(Collections.singleton("html"), "text/html;charset=utf-8");
        contentTypeMaps.put(Collections.singleton("xml"), "application/xml");
        contentTypeMaps.put(Collections.singleton("jpeg"), "image/jpeg");
        contentTypeMaps.put(Collections.singleton("gif"), "image/gif");
        contentTypeMaps.put(Collections.singleton("png"), "image/png");
        contentTypeMaps.put(Collections.singleton("pdf"), "application/pdf");
        contentTypeMaps.put(Collections.singleton("doc"), "application/msword");
    }

    /**
     * @param suffix 文件后缀
     * @return 返回contentType
     */
    public static String getContentTypeBySuffix(String suffix) {
        for (Set<String> supportSuffixList : contentTypeMaps.keySet()) {
            if (supportSuffixList.contains(suffix)) {
                return contentTypeMaps.get(supportSuffixList);
            }
        }
        return "text/plain;charset=utf-8";
    }
}
